/* new-sprite-sheet-dialog.vala
 *
 * Copyright (C) 2010, 2011  Richard Talbot-Watkins
 * Copyright (C) 2010, 2011  Dave Jeffery
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Authors:
 *  Richard Talbot-Watkins <richtw1@gmail.com>
 * 	Dave Jeffery <david.richard.jeffery@gmail.com>
 */

using Gtk;
using Gee;

/**
 * New sprite sheet dialog box for Beeb Spriter.
 *
 * @author Richard Talbot-Watkins <richtw1@gmail.com>
 * @author Dave Jeffery <david.richard.jeffery@gmail.com>
 * @version 2010.0115
 * @since 0.1
 */
public class BeebSpriter.NewSpriteSheetDialog : Dialog {
	/**
	 * List widget contains 1 column - the mode in column 0.
	 */
	private enum Columns {
		MODE,
		NUMBER_OF_COLUMNS
	}

	/**
	 * Stores list of BBC graphics modes and their descriptions.
	 */
	private HashMap<int, string> mode_list;

	/**
	 * Data model for the mode list widget.
	 */
	private ListStore store;

	/**
	 * Tree view widget to display the list of BBC graphics modes.
	 */
	private TreeView tree_view;

	/**
	 * Constructor for the new sprite sheet dialog box.
	 *
	 * @param parent_window Used to set set_transient and icon properties.
	 */
	public NewSpriteSheetDialog (Window parent_window) {
		/* Create a mode list. */
		create_mode_list ();

		/* Create a list store object. */
		store = create_store ();
		fill_store (store);

		/* Initialise dialog box. */
		title = _("New sprite sheet");
		border_width = 5;
		set_default_size (120, 200);
		icon = parent_window.icon;
		resizable = false;
		set_transient_for (parent_window);

		/* Add widgets to the dialog box. */
		create_widgets ();
	}

	/**
	 * Initialises a hash map of BBC screeen modes and their descriptions.
	 */
	private void create_mode_list () {
		mode_list = new HashMap<int, string> ();
		mode_list [0] = _("Mode 0 (2 colours, 640x256)");
		mode_list [1] = _("Mode 1 (4 colours, 320x256)");
		mode_list [2] = _("Mode 2 (16 colours, 160x256)");
		mode_list [4] = _("Mode 4 (2 colours, 320x256)");
		mode_list [5] = _("Mode 5 (4 colours, 160x256)");
	}

	/**
	 * Creates the ListStore used as the model for the mode picker widget.
	 *
	 * @return A ListStore instance set up for BBC screen mode data.
	 */
	private ListStore create_store () {
		return new ListStore (Columns.NUMBER_OF_COLUMNS, typeof (int));
	}

	/**
	 * Fills the ListStore used as a model for the widget.
	 *
	 * @param store The ListStore to fill with BBC screen mode numbers.
	 */
	private void fill_store (ListStore store) {
		TreeIter iter;

		foreach (int mode in mode_list.keys) {
			store.append (out iter);
			store.set (iter, Columns.MODE, mode);
		}
	}

	/**
	 * Sets a cell's text to the name of the mode stored in it's model.
	 *
	 * The parameter list is standard for cell renderer data functions.
	 *
	 * @param cell_layout Not used by this function.
	 * @param cell The cell of the TreeView whose text we want to set.
	 * @param tree_model The ListStore used as a model for the IconView.
	 * @param iter Points to the data for cell we're setting in the ListStore.
	 */
	private void set_mode_name (CellLayout cell_layout, CellRenderer cell, 
									TreeModel tree_model, TreeIter iter) {

		int mode;
		tree_model.get (iter, Columns.MODE, out mode);

		cell.set ("text", mode_list[mode]);
	}

	/**
	 * Creates the widgets that appear on the new sprite dialog box.
	 */
	private void create_widgets () {
		var message = new Label 
			(_("Select a BBC screen mode to apply to the new Sprite Sheet:"));

		tree_view = new TreeView.with_model (store);
		tree_view.headers_visible = false;

		var label_renderer = new CellRendererText ();
		var tree_view_column = new TreeViewColumn.with_attributes (
														null, label_renderer);
		tree_view_column.set_cell_data_func (label_renderer, set_mode_name);
		tree_view.insert_column(tree_view_column, -1);
		var selection = tree_view.get_selection ();
		var path = new TreePath.from_string ("2");
		selection.select_path (path);

		vbox.spacing = 8;
		vbox.pack_start (message, false, false, 4);
		vbox.pack_start (tree_view, false, false, 4);
		vbox.show_all ();

		add_button (STOCK_CANCEL, ResponseType.CANCEL);
		add_button (STOCK_APPLY, ResponseType.APPLY);
		set_default_response (ResponseType.APPLY);
	}

	/**
	 * Returns the mode selected by the user in the dialog box.
	 *
	 * @return Mode selected by the user as an int.
	 */
	public int get_selected_mode () {
		var selection = tree_view.get_selection ();
		TreeModel tree_model;
		TreeIter iter;
		selection.get_selected (out tree_model, out iter);

		int mode;
		tree_model.get (iter, Columns.MODE, out mode);

		return mode;
	}
}
